package sv.gob.mtps.hr.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import sv.gob.mtps.hr.model.SferhEmpleado;
import sv.gob.mtps.hr.model.SferhEmpleadoXCatParentesco;
import sv.gob.mtps.hr.service.EmpleadoXCatParentescoService;
import sv.gob.mtps.hr.service.RegistroPersonalService;

@Controller
public class ParentController {
	
	@Autowired
	EmpleadoXCatParentescoService parentescoService;
	
	@Autowired
	RegistroPersonalService personalService;
	
	@RequestMapping("/admin/showEmp")
	public String listEmpleado(Map<String, Object> map){
		map.put("emp", new SferhEmpleado());
		map.put("empList", personalService.listSferhEmpleado());
		return "/admin/catEmp";
	}
	
	@RequestMapping("/admin/showParent")
	public String listParent(@ModelAttribute("emp") @Valid SferhEmpleado empleado, BindingResult result, Map<String, Object> map){
		
		SferhEmpleadoXCatParentesco parent = new SferhEmpleadoXCatParentesco();
		SferhEmpleado e = (SferhEmpleado)personalService.findSferhEmpleado(empleado.getIdEmpleado());
		parent.setSferhEmpleado(e);
		System.out.println(parent.getSferhEmpleado().getNombreCompleto());
		map.put("genList", personalService.listGenero());
		map.put("listParentesco", parentescoService.listParentesco());
		map.put("benList", parentescoService.listBeneficio());
		map.put("parent", parent);
		map.put("parentList", parentescoService.listParent(parent.getSferhEmpleado().getIdEmpleado()));
		return "/admin/regParent";
	}
	
	@RequestMapping("/admin/showParentDet")
	public String listParentDet(@ModelAttribute("parent") @Valid SferhEmpleadoXCatParentesco parent, BindingResult result, Map<String, Object> map, HttpServletRequest request){
		
		String idEmpleado = null;
		idEmpleado = (String)request.getParameter("idEmpleado");
		if(idEmpleado == null){
			idEmpleado = String.valueOf(parent.getSferhEmpleado().getIdEmpleado());
			System.out.println("en el if ParentDet..." + idEmpleado);
		}
		
		System.out.println(idEmpleado);
		parent.setSferhEmpleado(personalService.findSferhEmpleado(Integer.parseInt(idEmpleado)));
		System.out.println(parent.getSferhEmpleado().getNombreCompleto());
		map.put("genList", personalService.listGenero());
		map.put("listParentesco", parentescoService.listParentesco());
		map.put("benList", parentescoService.listBeneficio());
		
		map.put("parentList", parentescoService.listParent(Integer.parseInt(idEmpleado)));
		return "/admin/regParent";
	}
	
	@RequestMapping("/admin/addParent")
	public String addParent(@ModelAttribute("parent") @Valid SferhEmpleadoXCatParentesco parent, BindingResult result, Map<String, Object> map){
		
		System.out.println(parent.getSferhEmpleado().getIdEmpleado());
		SferhEmpleado e = (SferhEmpleado)personalService.findSferhEmpleado(parent.getSferhEmpleado().getIdEmpleado());
		parent.setSferhEmpleado(e);
		System.out.println(parent.getSferhEmpleado().getNombreCompleto());
		//map.put("genList", personalService.listGenero());
		parentescoService.addParent(parent);
		
		return "redirect:/admin/showParentDet?idEmpleado="+parent.getSferhEmpleado().getIdEmpleado();
	}
	
	@RequestMapping("/admin/editParent")
	public String editParent(@ModelAttribute("parent") @Valid SferhEmpleadoXCatParentesco parent, BindingResult result, Map<String, Object> map){
		
		
		SferhEmpleado e = (SferhEmpleado)personalService.findSferhEmpleado(parent.getSferhEmpleado().getIdEmpleado());
		parent.setSferhEmpleado(e);
		System.out.println(parent.getFechaNacimientoFamiliar());
		//map.put("genList", personalService.listGenero());
		parentescoService.updateParent(parent);
		
		return "redirect:/admin/showParentDet?idEmpleado="+parent.getSferhEmpleado().getIdEmpleado();
	}
}
